package org.baicaix.chat.views;

import java.awt.Component;

import javax.swing.JTextArea;
import org.baicaix.chat.ChatConstants;

import org.puremvc.java.interfaces.INotification;
import org.puremvc.java.patterns.mediator.Mediator;

/**
 * @author Rocky Jiang
 * blog:www.blogjava.net/kissjava
 * */
public class InternalFrameMediator extends Mediator {

    public InternalFrameMediator(String mediatorName, Component viewComponent) {
        super(mediatorName, viewComponent);
    }

    private InternalFrame getFrame() {
        return (InternalFrame) viewComponent;
    }

    @Override
    public void handleNotification(INotification noti) {
        // TODO Auto-generated method stub
        super.handleNotification(noti);
        if (ChatConstants.RECEIVE_FROM_FRAME.equals(noti.getName())) {
            printMessage(noti);
        }
    }

    private void printMessage(INotification noti) {
        StringBuffer text = new StringBuffer(this.getLog().getText()).append("\n");
        text.append(noti.getType()).append(" : ").append(noti.getBody().toString());
        this.getLog().setText(text.toString());
    }

    private JTextArea getLog() {
        return getFrame().getLog();
    }

    @Override
    public String[] listNotificationInterests() {
        return new String[]{
                    ChatConstants.RECEIVE_FROM_FRAME
                };
    }

    public void send(String message) {
        facade.sendNotification(ChatConstants.SEND_TO_FRAME, message, getFrame().getTitle());
    }
}
